package com.weixin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.weixin.entity.User;
import com.weixin.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    private IUserService userService;

    @RequestMapping("/login")
    public String login(User user, HttpSession session){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",user.getUserName()).eq("password",user.getPassword());
        User user1 = userService.getOne(queryWrapper);
        if (user1!=null){
            //登陆成功
            session.setAttribute("user",user1);
        }
        return "redirect:/";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/";
    }
}
